//@version=6
indicator("Take Profit X", overlay=true, max_labels_count=500)
import TradingView/ta/10

// === User Inputs ===
group_ind = "Indicators"
useRSI = input.bool(true, "Use RSI", group=group_ind)
useMACD = input.bool(true, "Use MACD", group=group_ind)
useStoch = input.bool(true, "Use Stochastic", group=group_ind)
useBB = input.bool(true, "Use Bollinger Bands", group=group_ind)
useSTrend = input.bool(true, "Use Supertrend", group=group_ind)

// --- Added Indicators ---
useCCI = input.bool(true, "Use CCI", group=group_ind)
useEMA = input.bool(true, "Use EMA Cross", group=group_ind)

// Price Action Breakout Settings
usePriceAction = input.bool(true, "Use Price Action Breakout", group=group_ind)
lookBackBars = input.int(5, "Look Back Bars for Breakout", minval=1, maxval=50)

minConfirmations = input.int(5, "Minimum Confirmations for TP Signal", minval=1, maxval=8) // Adjusted max to 8

// === Indicator Settings ===
// RSI
rsiLen = input.int(14, "RSI Length", group="RSI Settings")
rsiOB = input.int(70, "RSI Overbought Level", group="RSI Settings")
rsiOS = input.int(30, "RSI Oversold Level", group="RSI Settings")
rsi = ta.rsi(close, rsiLen)

// MACD
fastLen = input.int(12, "MACD Fast Length", group="MACD Settings")
slowLen = input.int(26, "MACD Slow Length", group="MACD Settings")
signalLen = input.int(9, "MACD Signal Length", group="MACD Settings")
macd = ta.ema(close, fastLen) - ta.ema(close, slowLen)
macdSignal = ta.ema(macd, signalLen)
macdCrossUnder = ta.crossunder(macd, macdSignal)
macdCrossOver = ta.crossover(macd, macdSignal)

// Stochastic
kLen = input.int(14, "Stoch K Length", group="Stoch Settings")
dLen = input.int(3, "Stoch D Length", group="Stoch Settings")
smoothK = input.int(3, "Stoch Smooth K", group="Stoch Settings")
k = ta.sma(ta.stoch(close, high, low, kLen), smoothK)
d = ta.sma(k, dLen)
stochCrossUnder = ta.crossunder(k, d)
stochCrossOver = ta.crossover(k, d)

// Bollinger Bands
bbLen = input.int(20, "BB Length", group="BB Settings")
bbMult = input.float(2.0, "BB StdDev Multiplier", group="BB Settings")
basis = ta.sma(close, bbLen)
dev = bbMult * ta.stdev(close, bbLen)
upper = basis + dev
lower = basis - dev

// Supertrend
stAtrMult = input.float(3.0, "Supertrend ATR Multiplier", group="Supertrend Settings")
stAtrLen = input.int(10, "Supertrend ATR Length", group="Supertrend Settings")
[_, stDir] = ta.supertrend(stAtrMult, stAtrLen)

// CCI
cciLen = input.int(20, "CCI Length", group="CCI Settings")
cci = ta.cci(close, cciLen)
cciOB = input.int(100, "CCI Overbought", group="CCI Settings")
cciOS = input.int(-100, "CCI Oversold", group="CCI Settings")

// EMA Cross
emaFastLen = input.int(12, "EMA Fast Length", group="EMA Settings")
emaSlowLen = input.int(26, "EMA Slow Length", group="EMA Settings")
emaFast = ta.ema(close, emaFastLen)
emaSlow = ta.ema(close, emaSlowLen)
emaCrossBull = ta.crossover(emaFast, emaSlow)
emaCrossBear = ta.crossunder(emaFast, emaSlow)

// Price Action Breakout Conditions
priceBreakoutBull = close > close[lookBackBars]
priceBreakoutBear = close < close[lookBackBars]

// Bullish TP conditions
rsiBull = useRSI and (rsi > rsiOB)
macdBull = useMACD and macdCrossUnder
stochBull = useStoch and stochCrossUnder and k > 80
bbBull = useBB and close > upper
stBull = useSTrend and stDir == -1
cciBull = useCCI and (cci > cciOB)
emaBull = useEMA and emaCrossBear
priceActionBull = usePriceAction and priceBreakoutBull
// --------------------------------------------------------

// Bearish TP conditions
rsiBear = useRSI and (rsi < rsiOS)
macdBear = useMACD and macdCrossOver
stochBear = useStoch and stochCrossOver and k < 20
bbBear = useBB and close < lower
stBear = useSTrend and stDir == 1
cciBear = useCCI and (cci < cciOS)
emaBear = useEMA and emaCrossBull
priceActionBear = usePriceAction and priceBreakoutBear
// --------------------------------------------------------

// === Count confirmations ===
bullCount = (rsiBull ? 1 : 0) + (macdBull ? 1 : 0) + (stochBull ? 1 : 0) + (bbBull ? 1 : 0) + (stBull ? 1 : 0) + (cciBull ? 1 : 0) + (emaBull ? 1 : 0) + (priceActionBull ? 1 : 0)

bearCount = (rsiBear ? 1 : 0) + (macdBear ? 1 : 0) + (stochBear ? 1 : 0) + (bbBear ? 1 : 0) + (stBear ? 1 : 0) + (cciBear ? 1 : 0) + (emaBear ? 1 : 0) + (priceActionBear ? 1 : 0)

// === Final TP signals with mutual exclusion ===
bullTP = (bullCount >= minConfirmations) and (bullCount > bearCount)
bearTP = not bullTP and (bearCount >= minConfirmations) and (bearCount > bullCount)

// === Plot Labels ===
if bullTP
    label.new(bar_index, high, "", style=label.style_circle, color=color.rgb(0, 255, 8, 30), textcolor=color.white, size=size.tiny)
if bearTP
    label.new(bar_index, low, "", style=label.style_circle, color=#ff0000b2, textcolor=color.white, size=size.tiny)

// === Alerts ===
alertcondition(bullTP, title="Bullish Take Profit", message="Bullish Take Profit signal triggered")
alertcondition(bearTP, title="Bearish Take Profit", message="Bearish Take Profit signal triggered")